from loguru import logger
from sqlalchemy.orm import Session

from app.api import deps
from app.core.celery_app import celery_app
from app.core.constant_code import ConstantItem
from app.models.instrument import Instrument
from app.spider.binance.binance_bar_spider import BinanceBarSpider
from app.spider.binance.binance_calendar_spider import BinanceCalendarSpider
from app.spider.binance.binance_instrument_spider import BinanceInstrumentSpider


@celery_app.task(acks_late=True)
def calendarSpider():
    try:
        logger.info("BinanceSpiderTask -> calendarSpider......start")
        db: Session = next(deps.getDb())
        spider = BinanceCalendarSpider(db)
        spider.crawlCalendar()
        logger.info("BinanceSpiderTask -> calendarSpider......end")
    except Exception as ex:
        logger.exception("BinanceSpiderTask -> calendarSpider -> 交易所时间爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> calendarSpider......end")


@celery_app.task(acks_late=True)
def instrumentSpider():
    try:
        logger.info("BinanceSpiderTask -> instrumentSpider......start")
        db: Session = next(deps.getDb())
        spider = BinanceInstrumentSpider(db)
        spider.crawlInstrumentBasic()
        logger.info("BinanceSpiderTask -> instrumentSpider......end")
    except Exception as ex:
        logger.exception("BinanceSpiderTask -> instrumentSpider -> 交易对爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> calendarSpider......end")


@celery_app.task(acks_late=True)
def barSpider():
    try:
        logger.info("BinanceSpiderTask -> barSpider......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_CRYPTOCURRENCY,
                                                  Instrument.exchange == 'BINANCE',
                                                  Instrument.countryCode == 'GLOBAL').all()
        spider = BinanceBarSpider(db, instruments)
        spider.crawlDay()
        logger.info("BinanceSpiderTask -> barSpider......day end")
        spider.crawl1hour()
        logger.info("BinanceSpiderTask -> barSpider...... 1hour end")
    except Exception as ex:
        logger.exception("BinanceSpiderTask -> barSpider -> K线爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> barSpider......end")

def calendarSpiderJob():
    try:
        logger.info("BinanceSpiderTask -> calendarSpiderJob......start")
        db: Session = next(deps.getDb())
        spider = BinanceCalendarSpider(db)
        spider.crawlCalendar()
        logger.info("BinanceSpiderTask -> calendarSpiderJob......end")
    except Exception as ex:
        logger.exception("BinanceSpiderTask -> calendarSpiderJob -> 交易所时间爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> calendarSpiderJob......end")


def instrumentSpiderJob():
    try:
        logger.info("BinanceSpiderTask -> instrumentSpiderJob......start")
        db: Session = next(deps.getDb())
        spider = BinanceInstrumentSpider(db)
        spider.crawlInstrumentBasic()
        logger.info("BinanceSpiderTask -> instrumentSpiderJob......end")

    except Exception as ex:
        logger.exception("BinanceSpiderTask -> instrumentSpider -> 交易对爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> calendarSpider......end")


def barSpiderJob():
    try:
        logger.info("BinanceSpiderTask -> barSpiderJob......start")
        db: Session = next(deps.getDb())
        instruments = db.query(Instrument).filter(Instrument.marketType == ConstantItem.MARKET_CRYPTOCURRENCY,
                                                  Instrument.exchange == 'BINANCE',
                                                  Instrument.countryCode == 'GLOBAL').all()
        spider = BinanceBarSpider(db, instruments)
        spider.crawlDay()
        logger.info("BinanceSpiderTask -> barSpiderJob......day end")
        spider.crawl1hour()
        logger.info("BinanceSpiderTask -> barSpiderJob...... 1hour end")
    except Exception as ex:
        logger.exception("BinanceSpiderTask -> barSpiderJob -> K线爬取失败")
    finally:
        logger.info("BinanceSpiderTask -> barSpiderJob......end")